package com.aizhixin.lab.statistics.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.company.service.ClassesService;
import com.aizhixin.lab.course.classes.dto.StuReportDTO;
import com.aizhixin.lab.course.course.service.CourseService;
import com.aizhixin.lab.statistics.domain.*;
import com.aizhixin.lab.statistics.service.StudentService;
import com.aizhixin.lab.task.domain.StuTaskDomain;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/statistics")
@Api(value = "学生数据Api", description = "学生数据Api")
public class StudentDataController {
    @Autowired
    private AccountService accountService;

    @Autowired
    private StudentService studentService;
    @Autowired
    private ClassesService classesService;
    @Autowired
    private CourseService courseService;

    @RequestMapping(value = "/vitalityData", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "学生活跃度统计", response = Void.class, notes = "学生活跃度统计<br>@author lwq")
    public ResponseEntity<?> vitalityData(@RequestHeader("Authorization") String token,
                                         @ApiParam(value = "day", required = true) @RequestParam(value = "day") Integer day) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        StudentVitalityDomain studentVitalityDomain = studentService.vitalityData(day, account.getId());
        return new ResponseEntity(studentVitalityDomain, HttpStatus.OK);
    }
    @RequestMapping(value = "/getLongNum", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "用户登陆次数", response = Void.class, notes = "用户登陆次数<br>@author lwq")
    public ResponseEntity<?> getLongNum(@RequestHeader("Authorization") String token,
                                          @ApiParam(value = "day", required = true) @RequestParam(value = "day") Integer day) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        LoginNumDomain longNum = studentService.getLongNum(day, account.getId());
        return new ResponseEntity(longNum, HttpStatus.OK);
    }
    @RequestMapping(value = "/getCourseList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "实验课程统计", response = Void.class, notes = "实验课程统计<br>@author lwq")
    public ResponseEntity<?> getCourseList(@RequestHeader("Authorization") String token) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<ExperimentalStatisticsDomain> courseList = studentService.getCourseList(account.getId());
        return new ResponseEntity(courseList, HttpStatus.OK);
    }
    @RequestMapping(value = "/courseStudyCase", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "课程总体学习情况统计", response = Void.class, notes = "课程总体学习情况统计<br>@author lwq")
    public ResponseEntity<?> courseStudyCase(@RequestHeader("Authorization") String token,
                                           @ApiParam(value = "courseId", required = true) @RequestParam(value = "courseId") String courseId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        CourseStudyCaseDomain courseStudyCaseDomain = studentService.courseStudyCase(courseId, account.getId());
        return new ResponseEntity(courseStudyCaseDomain, HttpStatus.OK);
    }
    @RequestMapping(value = "/getReportScore", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "学生实验报告成绩统计", response = Void.class, notes = "学生实验课程统计<br>@author lwq")
    public ResponseEntity<?> getReportScore(@RequestHeader("Authorization") String token,
                                             @ApiParam(value = "courseId", required = true) @RequestParam(value = "courseId") String courseId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }

        List<ChapterReportDomain> reportScore = studentService.getReportScore(courseId, account.getId());
        return new ResponseEntity(reportScore, HttpStatus.OK);
    }
    @RequestMapping(value = "/getTaskAvgScore", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "任务成绩统计", response = Void.class, notes = "任务成绩统计<br>@author lwq")
    public ResponseEntity<?> getTaskAvgScore(@RequestHeader("Authorization") String token,
                                            @ApiParam(value = "courseId", required = true) @RequestParam(value = "courseId") String courseId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<ChapterReportDomain> reportScore = studentService.getTaskAvgScore(courseId, account.getId());
        return new ResponseEntity(reportScore, HttpStatus.OK);
    }
    @RequestMapping(value = "/getStuReportList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "学生报告数据统计页", response = Void.class, notes = "学生报告数据统计页<br>@author lwq")
    public ResponseEntity<?> getStuReportList(@RequestHeader("Authorization") String token,
                                             @ApiParam(value = "courseId", required = true) @RequestParam(value = "courseId") String courseId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<StuReportDTO> stuReportStatistics = studentService.getStuReportStatistics(courseId, account.getId());
        return new ResponseEntity(stuReportStatistics, HttpStatus.OK);
    }
    @RequestMapping(value = "/getStuTaskList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "学生任务统计页", response = Void.class, notes = "学生任务统计页<br>@author lwq")
    public ResponseEntity<?> getStuTaskList(@RequestHeader("Authorization") String token,
                                             @ApiParam(value = "courseId", required = true) @RequestParam(value = "courseId") String courseId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<StuTaskDomain> stuTaskStatistics = studentService.getStuTaskStatistics(courseId, account.getId());
        return new ResponseEntity(stuTaskStatistics, HttpStatus.OK);
    }
}
